
import {getDetail,addStatus,editStatus} from './gameStatusEditServ.js'
import { message } from 'antd';
//导入路由组件，用于跳转等
import { Link, hashHistory } from 'dva/router'
import { isEmpty, isArray, cloneDeep, merge, get, pick } from 'lodash'
const defultState = {
    loading: false,
    btnLoading: false,
    detail:{
        id:"",
        description:"",//游戏描述
        gameImgUrl:"",//游戏图片
        gameName:"",//游戏名称
    },
    imgModal:{
		visible:false,
		srcs:[],
	},
	isSubmit:true,
}

// 声明module
const tmpModule = {
    namespace: 'gameStatusEditModel',
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    //   dispatch({type: 'queryInfo', payload: {}}) // end dispatch
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
                    if (!!get(query, 'ststusId', '')) {
						dispatch({ type: 'app/setTabTitle', payload: { title: '编辑游戏类型', location } });
						dispatch({ type: 'getDetail', payload: {  id: query.ststusId } })
					} else {
						dispatch({ type: 'app/setTabTitle', payload: { title: '新增游戏类型', location } });
					}
                }
            })
        },
    },


    // 方法列表
    effects: {
        *getDetail({ payload }, { put, call, select }) {
			try {
				let { detail } = yield select(state => state[tmpModule.namespace])
				const { result, success, message} = yield call(getDetail, payload)
				if (success) {
					yield put({ type: 'updateStore', payload: { detail: result } })
				}
			} catch (e) {
				console.error(e)
			}
        },
        
        //提交表单
		*submit({ payload }, { put, call, select }) {
			try {
				let { detail} = yield select((state) => state[tmpModule.namespace])
				yield put({ type: 'updateStore', payload: { btnLoading: true } })
                const server = detail.id == ''?addStatus:editStatus;
                const { success,result } = yield call(server, { ...detail });
                if (success) {
                    message.success('保存游戏类型成功');
                    hashHistory.push('/home/advertisementAdmin/gameStatusList')
                }
                yield put({ type: 'updateStore', payload: { btnLoading: false } })
                
				
			} catch (e) {
				console.log(e)
			}

        },
        
    },

    // reducers: {
    //   updateStore(preState, action) {
    //     return Object.assign({}, preState, action.payload)
    //   },
    // }
    reducers: {
        updateStore(preState, action) {
            return { ...preState, ...action.payload };
        }
    }
}

export default tmpModule
